<template>
	<div :class="className" :style="{ height: height, width: width }"></div>
</template>

<script>
import echarts from 'echarts/lib/echarts';
// 引入柱状图
require('echarts/lib/chart/bar');
// 引入提示框和title组件，图例
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');
require('echarts/lib/component/legend');
require('echarts/lib/component/legendScroll'); //图例滚动

import resize from '@/utils/resize.js';

export default {
	mixins: [resize],
	props: {
		className: {
			type: String,
			default: 'Order-column-chart'
		},
		width: {
			type: String,
			default: '100%'
		},
		height: {
			type: String,
			default: '300px'
		},
		autoResize: {
			type: Boolean,
			default: true
		},
		chartData: {
			type: Object,
			required: true
		}
	},
	data() {
		return {
			chart: null
		};
	},
	watch: {
		chartData: {
			deep: true,
			handler(val) {
				this.setOptions(val);
			}
		}
	},
	mounted() {
		this.$nextTick(() => {
			this.initChart();
		});
	},
	beforeDestroy() {
		if (!this.chart) {
			return;
		}
		this.chart.dispose();
		this.chart = null;
	},
	methods: {
		initChart() {
			this.chart = echarts.init(this.$el);
			this.setOptions(this.chartData);
		},
		setOptions({ date, data, color,tips } = {}) {
			this.chart.setOption({
				title: {
					text: '',
					left: 10,
					top: 10
				},

				grid: {
					right: '30px',
					left: '60px',
					top: '20px',
					bottom: '40px'
				},
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						type: 'shadow'
					}
				},
				/* legend: {
            left: '60%',
            top: '40px',
            data: ['销售数']
          }, */
				xAxis: {
					name: '月份',
					triggerEvent: true,
					data: date,
					axisLabel: {
						interval: 0,
						show: true,
						textStyle: {
							color: '#333'
						}
					},
					axisLine: {
						lineStyle: {
							show: false,
							color: '#F3F3F3',
							width: 2
						}
					}
				},
				yAxis: [
					{
						/* name: '销售数',
						type: 'value',
						nameTextStyle: {
							color: '#AEA4A8'
						}, */
						axisLabel: {
							interval: 0,
							show: true,
							textStyle: {
								color: '#333',
							}
						},
						axisLine: {
							lineStyle: {
								color: '#F3F3F3',
								width: 2
							}
						},
						splitLine: {
							lineStyle: {
								color: '#F3F3F3',
								width: 2
							}
						}
					},
					{
						nameTextStyle: {
							color: '#AEA4A8'
						},
						axisLabel: {
							interval: 0,
							show: false,
							textStyle: {
								color: '#B4B5BE'
							}
						},
						axisLine: {
							lineStyle: {
								color: '#F3F3F3',
								width: 2
							}
						},
						splitLine: {
							show: false,
							lineStyle: {
								color: '#F3F3F3',
								width: 2
							}
						}
					}
				],
				series: [
					{
						name: tips,
						type: 'bar',
						silent: true,
						itemStyle: {
							normal: {
								color: color
							}
						},
						data: data
					}
				]
			});
		}
	}
};
</script>
